package com.ehais.util;
import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

//The class to deal with the OkHttpCallback
public abstract class OkHttpCallBack implements Callback {

//    public abstract void onSuccess(final Call call, JSONObject jsonObject) throws JSONException;
    public abstract void onSuccess(String json) throws JSONException;

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        Log.d("OkHttpCallBackResponse：",call.toString()+"\r\nreponse"+response.toString()+"\r\nresponse.headers:"+response.headers() +"\r\nreponse.message:"+response.message());
        if (response != null) {
            String str = response.body().string().trim();

            Log.d("OkHttpCallBackResponse","接口结果:"+str);
            if (response.isSuccessful()) {
                try{
                    onSuccess(str);
//                    JSONObject object = (JSONObject)new JSONTokener(str).nextValue();
//                    if (object != null) {
//                        onSuccess(call, object);
//                    }else{
//                        onFailure(call, null);
//                    }
                }catch(JSONException e) {
                    e.printStackTrace();
                    onFailure(call, null);
                }

            }else{
                onFailure(call, null);
            }
        }
    }


    @Override
    public void onFailure(Call call, IOException e){
        Log.d("OkHttpCallBackFail",call.toString()+"//"+e.toString());
    }
}
